package com.fishery.memo.controller;

import com.fishery.entity.Result;
import com.fishery.memo.entity.Task;
import com.fishery.memo.entity.vo.TaskVo;
import com.fishery.memo.service.TaskService;
import com.fishery.memo.util.ConvertUtil;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "操作备忘录接口")
@RestController
@CrossOrigin
@RequestMapping("/memo/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private ConvertUtil convertUtil;

    @PostMapping("/")
    @ApiOperation("添加定时任务")
    public Result createTask(@RequestBody @ApiParam(required = true,value = "taskVo") TaskVo taskVo){
        return taskService.createTask(convertUtil.convert(taskVo,Task.class))
                ? Result.success("添加成功") : Result.error("添加失败");
    }

    @PutMapping("/")
    @ApiOperation("修改定时任务")
    public Result updateTask(@RequestBody @ApiParam(required = true,value = "taskVo") TaskVo taskVo){
        return taskService.updateTask(convertUtil.convert(taskVo,Task.class))
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除定时任务")
    public Result deleteTask(@PathVariable(value = "id") String taskId){
        return taskService.deleteTask(taskId)
                ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询用户的全部定时任务")
    public Result searchTask(@PathVariable(value = "id") String userId){
        return Result.success()
                .data(convertUtil.convert(taskService.searchTask(userId),TaskVo.class));
    }

    @GetMapping("/{id}/{page}/{size}")
    @ApiOperation("分页获取用户的定时任务列表")
    public Result searchTask(@PathVariable(value = "id") String userId
                            ,@PathVariable(value = "page") Integer page
                            ,@PathVariable(value = "size") Integer size){
        return Result.success()
                .data(convertUtil.convert(taskService.searchTask(userId, page, size),TaskVo.class));
    }

    @PutMapping("/{id}")
    @ApiOperation("暂停定时任务")
    public Result pauseTask(@PathVariable(value = "id") String taskId){
        return taskService.pauseTask(taskId)
                ? Result.success("暂停成功") : Result.error("暂停失败");
    }

    @PostMapping("/{id}")
    @ApiOperation("恢复定时任务")
    public Result resumeTask(@PathVariable(value = "id") String taskId){
        return taskService.resumeTask(taskId)
                ? Result.success("恢复成功") : Result.error("恢复失败");
    }
}
